package com.abhinav.concurrency;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.Semaphore;

public class ConnectionLimitor {
	private final Semaphore semaphore;

	private ConnectionLimitor(int maxConcurrentRequest) {
		semaphore = new Semaphore(maxConcurrentRequest);
	}

	public URLConnection acquire(URL url) throws InterruptedException,
			IOException {
		semaphore.acquire();

		return url.openConnection();
	}
	
	public void release(URLConnection url){
		try {
			
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			semaphore.release();
		}
		
	}

}
